package de.herberlin.server.httpd;

import java.io.*;
import java.util.logging.Logger;

import de.herberlin.server.common.*;
import de.herberlin.server.common.ConfigConstants;
import de.herberlin.server.common.Configuration;
import de.herberlin.server.common.event.EventDispatcher;
import de.herberlin.wwwutil.ResponseHeader;
import de.herberlin.wwwutil.SimpleResponse;
import de.herberlin.wwwutil.httperror.*;

/**
 * Class handling simple Http Request
 * @author hans joachim herbertz
 * created 28.01.2004
 */
public class BasicHttpHandler {

	/**
	 * All data from the thread;
	 */
	protected HttpData data = null;
	/**
	 * Logger
	 */
	protected Logger logger = Logger.getLogger(getClass().getName());

	/**
	 * The pause as coming from configuration
	 */
	protected int pause =
		(Configuration.getPrefs().getInt(ConfigConstants.MODE_HTTP+ConfigConstants.SETTING_DELAY, 0)
			* EventDispatcher.getNoConnections()
			* 10);

	/**
	 * Constructor
	 * @param data
	 */
	public BasicHttpHandler(HttpData data) {
		this.data = data;
	}

	/**
	 * Call this to start the process.
	 * @throws HttpError
	 */
	public void perform() throws HttpError {
		data.resp = new SimpleResponse();
		data.resp.setHeader("Content-Type", data.mimeType);
		data.resp.setHeader("Content-Length", data.realPath.length() + "");
		moreHeaders(data.resp);

		try {
			data.resp.write(data.out);
			writePaused(new FileInputStream(data.realPath), data.out, pause);

			// set display data
			data.fileData.setFile(data.realPath);
			data.fileData.setContentType(data.mimeType);
			data.fileData.setContentLength(data.realPath.length());

		} catch (FileNotFoundException e1) {
			InternalServerError_500 ex = new InternalServerError_500(e1);
			logger.fine(ex + "");
			throw ex;
		} catch (IOException e1) {
			ConnectionLost ex = new ConnectionLost(e1);
			logger.fine(ex + "");
			throw ex;
		}

	}

	/**
	 * Writes inputstream to the outputsteam using pause in ms.
	 * @param in
	 * @param out
	 * @throws IOException
	 */
	protected void writePaused(InputStream in, OutputStream out, long pause)
		throws IOException {
		byte[] buffer = new byte[256];
		int rest = -1;
		while ((rest = in.read(buffer)) != -1) {
			if (pause > 0) {
				try {
					Thread.sleep(pause);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
			out.write(buffer, 0, rest);
			out.flush();
		}
	}
	/**
	 * Sets additional headers and the no cache headers
	 * to the response if configuration
	 * requires it; if caching is allowed does nothing.
	 */
	protected void moreHeaders(ResponseHeader resp) {

		resp.setHeader("Server", ConfigConstants.VERSION);
		resp.setHeader("Connection", "close");

		if (Configuration
			.getPrefs()
			.getBoolean(ConfigConstants.HTTP_NO_CACHING_HEADERS, true)) {
			resp.setHeader("Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
			resp.setHeader(
				"Cache-Control",
				"no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
			resp.setHeader("Pragma", "no-cache");
		}
	}

}
